<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.appspot.niehonglei.jdo.Comment" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.appspot.niehonglei.dao.PMF" %>
<%@ page import="com.appspot.niehonglei.common.Tools" %>
<%
	/**
	 * 
	 */


	if(!"niehonglei".equals(session.getAttribute("logName"))){
		response.sendRedirect("/");
		return;
	}
	/**
	 * 
	 */	
	String footAction = "comment/";
	String pageNo = request.getParameter("_page");
	int intPageNo = 1;
	int recordsPerPage = 20;
	try{
		intPageNo = Integer.parseInt(pageNo);
	} catch (Exception e){
		//e.printStackTrace();
	}
	int totalRecords = Tools.countComment(null);
	//防止传入的参数为负数
	if(intPageNo <= 0){
		intPageNo = 1;
	}
	
	//计算总页数
	int totalPageNo = 1;		
	if(totalRecords % recordsPerPage == 0){
		totalPageNo = totalRecords / recordsPerPage;
	} else {
		totalPageNo = (totalRecords / recordsPerPage) + 1;
	}
	//防止总记录数为0
	if(totalPageNo == 0){
		totalPageNo = 1;
	}
	//控制是否在正常的上限范围内
	if((intPageNo - 1) * recordsPerPage > totalRecords){
		intPageNo = totalPageNo;
	}
	
	
	//生成页码
	String footCode = "";
	//小于5 直接生成1 2 3 4 5
	if(totalPageNo <= 8){
		for(int i=0;i<totalPageNo;i++){
			footCode += "<a href='/" + footAction + "page/"  + (i + 1) + "' class='blackText'>" + (i + 1) + "</a> ";
		}			
	}
	
	if(totalPageNo > 8 && intPageNo < 5){
		for(int i=0;i<5;i++){
			footCode += "<a href='/" + footAction + "page/"  + (i + 1) + "' class='blackText'>" + (i + 1) + "</a> ";
		}
		footCode += "<a href='/" + footAction + "page/"  + ((totalPageNo - 5)/2 + 5) + "' class='blackText'>...</a> ";
		footCode += " <a href='/" + footAction + "page/"  + totalPageNo + "' class='blackText'>末页</a>";			
	}		
	//页数大于5且当前页大于5小于totalPageNo - 5 显示成 首页 ... 6 7 8 9 10 ... 末页 1 ... 3 4 5 6 7 ... end
	else if(totalPageNo > 8 && intPageNo >= 5 && (intPageNo <= totalPageNo - 4)){
		footCode +=  "<a href='/" + footAction + "page/"  + 1 + "' class='blackText'>首页</a> ";
		footCode += "<a href='/" + footAction + "page/"  + ((intPageNo-2)/2 + 1) + "' class='blackText'>...</a> ";
		for(int i=0;i<5;i++){
			footCode += "<a href='/" + footAction + "page/"  + (intPageNo + i - 2) + "' class='blackText'>" + (intPageNo + i - 2) + "</a> ";
		}
		footCode += "<a href='/" + footAction + "page/"  + ((totalPageNo - intPageNo)/2 + 1 + intPageNo) + "' class='blackText'>...</a> ";
		footCode += " <a href='/" + footAction + "page/"  + totalPageNo + "' class='blackText'>末页</a>";
	}
	
	//页数大于5且当前页大于totalPageNo - 5 显示成 首页 ... 19 20 21 22 23
	else if(totalPageNo > 8 && (intPageNo > totalPageNo - 4)){
		footCode +=  "<a href='/" + footAction + "page/"  + 1 + "' class='blackText'>首页</a> ";
		footCode += "<a href='/" + footAction + "page/"  + ((totalPageNo - 4)/2) + "' class='blackText'>...</a> ";
		for(int i=0;i<5;i++){
			footCode += " <a href='/" + footAction + "page/"  + (totalPageNo + i - 4) + "' class='blackText'>" + (totalPageNo + i - 4) + "</a> ";
		}			
	}
	String range = " range " + (intPageNo - 1) * recordsPerPage + "," + intPageNo * recordsPerPage;	
	/**
	 * 
	 */	
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>留言列表</title>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	font-size: 12px;
	line-height: 20px;
}
.sideTitle {
	font-size: 14px;
	font-weight: bold;
	color: #E25209;
}
.basicInfo {
	color: #999;
}
.whiteText {
	color: #FFF;
}
.blackText {
	color: #000;
}
.title {
	font-size: 18px;
	font-weight: bold;
	color: #E25209;
}

a.whiteText{

}
a{
	text-decoration:none;
}
a:hover{
	text-decoration:underline;
}

-->
</style>
</head>

<body>
  <input type="button" name="publisharticle" id="publisharticle" onclick="location.href='/publish?new'" value="发布文章" />
  <input type="button" name="indexrecommend" id="indexrecommend" onclick="location.href='/common?new&type=0'"  value="首页推荐" />
  <input type="button" name="friendlinks"  id="friendlinks" onclick="location.href='/common?new&type=1'"  value="友情链接" />
  <input type="button" name="publisharticle" id="publisharticle" onclick="location.href='/comment'" value="管理评论"  disabled="disabled"/>
<input type="button" name="button1" value="首页" onclick="location.href='/'"/>
<input type="button" name="button22" onclick="location.href='/logout'" value="登出" />
  <%
  		PersistenceManager pm = PMF.get().getPersistenceManager();
		String query = "select from " + Comment.class.getName()
				+ " order by createDate desc" + range;
		List<Comment> list = (List<Comment>) pm.newQuery(query)
				.execute();
		if(list==null) return;
		for(int i=0;i<list.size();i++){
			Comment comment = list.get(i);
			StringBuffer outComment = new StringBuffer("<br>");
			outComment.append(comment.getId().intValue());
			outComment.append("：");
			outComment.append(comment.getNickName());
			outComment.append("，邮件：");
			outComment.append(comment.getEmail());
			outComment.append("，个人主页：");
			outComment.append(comment.getPersonalHomePage());
			outComment.append("，ip地址：：");
			outComment.append(comment.getIpAddress());
			outComment.append("，是否发布：");
			outComment.append(comment.getIfPublish());
			outComment.append("（创建时间：");
			outComment.append(Tools.date2str(comment.getCreateDate()));
			outComment.append("）<input onclick='javascript:location.href=\"/action/deleteComment&id=");
			outComment.append(comment.getId().intValue());
			outComment.append("\";' type='button' value='删除'/><p>");
			outComment.append(comment.getContent().getValue());
			outComment.append("</p>");
			out.println(outComment.toString());
		}
		pm.close();
  %>
  <p>当前是第<%= intPageNo %>页 共<%= totalPageNo %>页 &nbsp;<%= footCode %>
</body>
</html>
